<template>
  <div class="divBox">
    <el-card class="box-card">
      <zb-parser :form-id="formId" :is-create="isCreate" :edit-data="editData" @submit="handlerSubmit"
        @resetForm="resetForm" v-if="isShow && checkPermi(['admin:pass:appget'])" />
    </el-card>
  </div>
</template>

<script>
  import zbParser from '@/components/FormGenerator/components/parser/ZBParser';
  import {
    configInfo,
    passAppSaveApi,
    passAppInfoApi,
    passAppOtherSaveApi,
    passAppOtherInfoApi,
  } from '@/api/systemConfig.js';
  import {
    checkPermi
  } from '@/utils/permission'; // 权限判断函数
  export default {
    name: 'onePassConfig',
    components: {
      zbParser
    },
    data() {
      return {
        isShow: true,
        isCreate: 0,
        editData: {},
        formId: 144, //一号通配置 144
      };
    },
    // watch:{
    //   '$route.query.id'(newVal, oldVal){
    //     console.log(newVal,oldVal)
    //     this.formId=this.$route.query.id
    //   }
    // },
    mounted() {
      console.log(this.$route.query)
      // 在组件中获取当前路由对象并更改 meta.title
      // const route = this.$router.currentRoute;
      // this.$set(route.meta, 'title', 'New Title');

      if (this.$route.query.id) {
        this.formId = parseInt(this.$route.query.id)
        console.log(this.formId)
      }

      if (checkPermi(['admin:pass:appget'])) this.getPassAppInfo();
    },
    methods: {
      checkPermi,
      resetForm(formValue) {
        this.isShow = false;
      },
      handlerSubmit(data) {
        console.log('formId--->', this.formId)
        let formParams = []
        // 你需要传入数组：[{"name":"money01","value":"100"},{"name":"money01","value":"100"},...]
        for (const key in data) {
          const value = data[key];
          console.log(key); // 输出：'key'
          console.log(value); // 输出：'value'
          let obj = {
            name: key,
            value: value
          }
          formParams.push(obj)
        }
        // console.log(formParams)
        if (this.formId == 144) { //一号通的保存
          passAppSaveApi(data).then((res) => {
            this.getPassAppInfo();
            this.$message.success('操作成功');
          });
        } else { //设置其它三个配置的保存
          let params = {
            id: this.formId,
            fields: formParams
          }
          passAppOtherSaveApi(params).then((res) => {
            debugger
            this.getPassAppInfo();
            this.$message.success('操作成功');
          });
        }
      },
      //获取配置详情
      getPassAppInfo() {

        if (this.formId == 144) {
          passAppInfoApi().then((res) => {
            this.isShow = false;
            this.editData = res;
            this.isCreate = 1;
            setTimeout(() => {
              // 让表单重复渲染待编辑数据
              this.isShow = true;
            }, 80);
          });
        } else {
          let params = {
            formId: this.formId
          }
          passAppOtherInfoApi(params).then((res) => {
            this.isShow = false;
            this.editData = res;
            this.isCreate = 1;
            setTimeout(() => {
              // 让表单重复渲染待编辑数据
              this.isShow = true;
            }, 80);
          });



        }


      },
    },
  };
</script>

<style scoped></style>
